import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_pagewise/flutter_pagewise.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:loxen/mode/inspiration/inspiration_info_bean.dart';
import 'package:loxen/mode/mine/mine_comment_list.dart';
import 'package:loxen/mode/study/study_info_bean.dart';
import 'package:loxen/routes/routes.dart';
import 'package:loxen/utils/format_time.dart';
import 'package:loxen/utils/string_res.dart';
import 'package:loxen/viewmode/base_view_model.dart';
import 'package:loxen/viewmode/mine_comments_viewmodel.dart';
import 'package:loxen/widget/empty_widget.dart';
import 'package:loxen/widget/fail_widget.dart';
import 'package:loxen/widget/loading_widget.dart';
import 'package:loxen/widget/my_subassembly.dart';
import 'package:loxen/widget/triangle_painter.dart';
import 'package:provider/provider.dart';

class MineCommentWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<MineCommentViewModel>(
      create: (_) {
        var model = MineCommentViewModel();
        model.loadData();
        return model;
      },
      child: Scaffold(
        appBar: PreferredSize(
          preferredSize: Size(double.infinity, 95.h),
          child: Container(
            color: Theme.of(context).backgroundColor,
            child: SafeArea(
              child: Column(
                children: <Widget>[
                  Row(
                    children: <Widget>[
                      MySubassembly.getBackIcon(context,
                          color: Theme.of(context).textTheme.subtitle2.color),
                      Expanded(
                        child: Center(
                          child: Text(
                            StringResources.wo_de_ping_lun,
                            style: TextStyle(
                                fontSize: 32.sp,
                                fontWeight: FontWeight.w500,
                                color: Theme.of(context).textTheme.subtitle2.color),
                          ),
                        ),
                      ),
                      MySubassembly.getSearchIcon(null, text: Text('')),
                    ],
                  )
                ],
              ),
            ),
          ),
        ),
        body: Selector<MineCommentViewModel, BaseState>(
            builder: (context, state, _) {
              if (state == BaseState.LOADING) {
                return LoadingWidget();
              }
              if (state == BaseState.CONTENT) {
                PagewiseLoadController pageLoadController = PagewiseLoadController<MineComment>(
                    pageFuture: (page) =>
                        Provider.of<MineCommentViewModel>(context, listen: false).nextData(page),
                    pageSize: 10);
                return Container(
                  decoration: BoxDecoration(
                    border: Border(top: BorderSide(width: 1.h, color: Color(0xFFEBECF1))),
                    color: Theme.of(context).backgroundColor,
                  ),
                  padding: EdgeInsets.fromLTRB(18, 3, 18, 0),
                  child: RefreshIndicator(
                    onRefresh: () async {
                      Provider.of<MineCommentViewModel>(context, listen: false).refresh();
                      pageLoadController.reset();
                    },
                    child: PagewiseListView<MineComment>(
                        pageLoadController: pageLoadController,
                        loadingBuilder: (_) => MySubassembly.getLoadingItem(),
                        noItemsFoundBuilder: (_) => MySubassembly.getNullItem(msg: '没有更多评论'),
                        itemBuilder: (context, item, index) {
                          return Container(
                            decoration: BoxDecoration(
                                border: Border(
                                    bottom: BorderSide(width: 1.w, color: Color(0xFFEBECF1)))),
                            margin: EdgeInsets.only(top: 20.w),
                            padding: EdgeInsets.only(bottom: 20.w),
                            child: _commentRow(context, item),
                          );
                        }),
                  ),
                );
              }

              if (state == BaseState.EMPTY) {
                return EmptyWidget();
              }
              if (state == BaseState.FAIL) {
                return FailWidget();
              }
              return Text('');
            },
            selector: (_, model) => model.mState),
      ),
    );
  }

  Widget _commentRow(BuildContext context, MineComment model) {
    return Column(
      children: <Widget>[
        Row(
          children: <Widget>[
            Container(
                margin: EdgeInsets.only(right: 10),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(50),
                ),
                child: CircleAvatar(
                  radius: 22,
                  backgroundImage: NetworkImage(model.user.portrait),
                )),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: <Widget>[
                      Row(
                        children: <Widget>[
                          Padding(
                            padding: EdgeInsets.only(right: 4),
                            child: Text(
                              model.user.nickname,
                              style: TextStyle(
                                  color: Color(0xFF000000),
                                  fontSize: 28.sp,
                                  fontWeight: FontWeight.bold),
                            ),
                          ),
                          Text(
                            '评论',
                            style: TextStyle(
                              color: Color(0xFF999999),
                              fontSize: 28.sp,
                            ),
                          )
                        ],
                      ),
                      Text(
                        FormatTime.readTimestamp(model.addTime),
                        style: TextStyle(
                            color: Color(0xFF999999), fontSize: 20.sp, fontWeight: FontWeight.w500),
                      )
                    ],
                  ),
                  Container(
                    margin: EdgeInsets.only(top: 2),
                    child: Text(
                      model.content,
                      style: TextStyle(
                          fontSize: 24.sp, color: Color(0xFF000000), fontWeight: FontWeight.w500),
                    ),
                  )
                ],
              ),
            )
          ],
        ),
        GestureDetector(
          onTap: () {
            _detailRouter(context, model.instanceId, model.contentType);
          },
          child: Stack(
            children: <Widget>[
              Container(
                padding: EdgeInsets.all(9),
                margin: EdgeInsets.only(top: 14),
                color: Color(0xFFF6F6F7),
                child: Row(
                  children: <Widget>[
                    Container(
                      margin: EdgeInsets.only(right: 10),
                      child: Image.network(
                        model.cover,
                        width: 79,
                        height: 79,
                        fit: BoxFit.cover,
                      ),
                    ),
                    Expanded(
                      child: Container(
                        padding: const EdgeInsets.only(top: 6, bottom: 6.5),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: <Widget>[
                            Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: <Widget>[
                                Container(
                                  margin: EdgeInsets.only(bottom: 2),
                                  child: Text(
                                    model.title,
                                    overflow: TextOverflow.ellipsis,
                                    maxLines: 1,
                                    style: TextStyle(
                                        color: Color(0xFF000000),
                                        fontSize: 22.sp,
                                        fontWeight: FontWeight.bold),
                                  ),
                                ),
                                Text(
                                  model.brief,
                                  overflow: TextOverflow.ellipsis,
                                  maxLines: 2,
                                  style: TextStyle(
                                      color: Color(0xFF82868E),
                                      fontSize: 18.sp,
                                      fontWeight: FontWeight.w500),
                                )
                              ],
                            ),
                            Container(
                              margin: EdgeInsets.only(),
                              child: Row(
                                children: <Widget>[
                                  Container(
                                      width: 24,
                                      height: 24,
                                      decoration: BoxDecoration(
                                          shape: BoxShape.circle,
                                          image: DecorationImage(
                                              image: NetworkImage(
                                                model.infoUser.portrait,
                                              ),
                                              fit: BoxFit.cover))),
                                  Padding(
                                    padding: EdgeInsets.only(left: 7),
                                    child: Text(
                                      model.infoUser.nickname,
                                      style: TextStyle(color: Color(0xFF82868E), fontSize: 24.sp),
                                    ),
                                  )
                                ],
                              ),
                            )
                          ],
                        ),
                      ),
                    )
                  ],
                ),
              ),
              Positioned(
                  top: 2,
                  left: 54.0,
                  child: CustomPaint(
                    painter: TrianglePainter(
                      strokeColor: Color(0xFFF6F6F7),
                      strokeWidth: 10,
                      paintingStyle: PaintingStyle.fill,
                    ),
                  )),
            ],
          ),
        )
      ],
    );
  }
}

// 跳转到详情
_detailRouter(BuildContext context, String id, String type) {
  switch (type) {
    case 'idea-article':
      InspirationInfo myWorks = InspirationInfo();
      myWorks.id = int.parse(id);
      Navigator.of(context).pushNamed(Routes.inspiration_detail, arguments: myWorks);
      break;
    case 'study-knowledge':
      StudyInfo entity = StudyInfo();
      entity.id = int.parse(id);
      int category = 0;
      Navigator.of(context).pushNamed(Routes.study_detail,
          arguments: {"detail": entity, "currentCategory": category});
      break;
    default:
  }
}
